
/**
 * 存储一些页面数据
 * @type {Vue}
 */
var data = new Vue({
    el:'#vue-el-data',
    data:{
        page: 1,  //当前页数,默认为第一页
        size: 3, //每页显示的数据条数,默认为3条
        totalPage: null, //总页数,默认为null
        cardName: '', //搜索关键字
    },
    methods:{
        setTotalPage:function (totalpage) {
            this.totalPage = totalpage;
            setPage.totalPage = totalpage;
        },
        setCardName:function (cardname) {
            this.cardName = cardname;
        }
    },
    mounted(){

    }

})

var params = new URLSearchParams();

/**
 * 传参
 */


/**
 * 通过ajax 获取需要的json数据 和 向后台传递参数
 * @type {Vue}
 */
var info =new Vue({
    el:'#cardPaging',
    data:{
        cardPaging: {},
    },
    methods:{
        //将message改为空
        setMessageNull:function(){
            deleteCard.message = null;
            updateCard.message = null;
        },
        //绑定当前需要更新前的数据
        setDeleteData:function(cardId){
            deleteCard.cardId = cardId;
        },
        setUpdateData:function(cardId,cardAddress,cardName,cardChancef,cardChancel
            ,cardChance,cardLevel,gameName){
            updateCard.cardId=cardId;
            updateCard.cardAddress=cardAddress;
            updateCard.cardName=cardName;
            updateCard.cardChancef=cardChancef;
            updateCard.cardChancel=cardChancel;
            updateCard.cardChance=cardChance;
            updateCard.cardLevel=cardLevel;
            updateCard.gameName=gameName;
        },

        setCardPaging:function(cardPaging){
            this.cardPaging = cardPaging;
        },
        setParams: function(page,size){
            params.append('page',page);
            params.append('size',size);
            return params;
        },
        getCardPaging: function (params) {
            axios
                .post('/ajax/cardmanage',params)
                .then(function (response){
                    info.setCardPaging(response);
                    data.totalPage = info.cardPaging.data.paging.totalPage;
                    data.cardName = info.cardPaging.data.paging.cardName;
                    data.page = info.cardPaging.data.paging.page;
                    data.size = info.cardPaging.data.paging.size;
                    setSize.size=data.size;
                    setPage.page = data.page;
                    setPage.totalPage = data.totalPage;
                })
                .catch(function (error) { // 请求失败处理
                    console.log(error);
                });
        }
    },
    mounted(){
        //设置页数和大小
        console.log("自定义错误类型1:");
        this.setParams(data.page,data.size);
        //刷新页面时,自动获取数据,根据默认值
        axios
            .post('/ajax/cardmanage',params)
            .then(function (response){
                info.setCardPaging(response);
                data.totalPage = info.cardPaging.data.paging.totalPage;
                data.cardName = info.cardPaging.data.paging.cardName;
                data.page = info.cardPaging.data.paging.page;
                data.size = info.cardPaging.data.paging.size;
                setSize.size=data.size;
                setPage.page = data.page;
                setPage.totalPage = data.totalPage;
            })
            .catch(function (error) { // 请求失败处理
                console.log(error);
            });
    }
})




/**
 * 修改页数
 */
var setPage = new Vue({
    el: '#vue-el-setpage',
    data: {
        totalPage: data.totalPage,
        page: data.page,
    },
    methods: {
        /**
         * 设置跳转的页数
         * @param page
         */
        setPage: function(page){
            params.delete('size');
            params.delete('page');
            params.append('page',page);
            params.append('size',data.size);
            info.getCardPaging(params);
        }

    }
})



/**
 * 修改页面大小
 */
var setSize = new Vue({
    el:'#vue-el-setsize',
    data:{
        size: null,
    },
    methods:{
        /**
         *      改变每页显示条数
         * @param size
         */
        setSize: function () {
            params.delete('size');
            params.delete('page');
            params.delete('cardName');

            params.append('page',1);
            if(data.cardName==null)
                params.append('cardName',"");
            else
                params.append('cardName',data.cardName);
            if(this.size != null && this.size>=1){
                params.append('size',this.size);
                info.getCardPaging(params);
            }
        },
    }
})



/**
 * 查询数据
 */
var search = new Vue({
    el:'#vue-el-search',
    data:{
        cardName: '',
    },
    methods:{
        /**
         * 根据输入的关键字进行查询
         * @param cardName
         */
        setSearchName: function () {
            params.delete('size');
            params.delete('page');
            params.delete('cardName');
            params.append('size',data.size);
            params.append('page',1);
            params.append('cardName',this.cardName);
            info.getCardPaging(params);
        },
    }

})

/**
 * 增加卡片的逻辑
 */
var addCard = new Vue({
    el:'#vue-el-addCard',
    data:{
        cardId:null,
        cardAddress:null,
        cardName:null,
        cardChancef:null,
        cardChancel:null,
        cardChance:null,
        cardLevel:null,
        gameName:null,
        message:null,
    },
    methods:{
        deleteParams: function(){
            params.delete("cardId");
            params.delete("cardChancef");
            params.delete("cardChancel");
            params.delete("cardChance");
            params.delete("cardLevel");
            params.delete("gameName");
            params.delete("cardAddress");
            params.delete("cardName");
        },
        setParams: function(){
            params.append("cardName",this.cardName);
            params.append("cardAddress",this.cardAddress);
            if(this.cardId != null)
                params.append("cardId",this.cardId);
            if(this.cardChancef != null)
                params.append("cardChancef",this.cardChancef);
            if(this.cardChancel != null)
                params.append("cardChancel",this.cardChancel);
            params.append("cardChance",this.cardChance);
            params.append("cardLevel",this.cardLevel);
            params.append("gameName",this.gameName);
        },

        addCardinfo: function(){
            this.deleteParams();
            this.setParams();
            //向后台传递数据, 增加数据
            this.getCardPaging(params);
        },
        getCardPaging: function (params) {
            axios
                .post('/ajax/card/insert',params)
                .then(function (response){
                    info.cardPaging.data.message = response.data.message;
                    addCard.message = response.data.message;
                    //刷新页面,转跳到第一页
                    addCard.deleteParams();
                    setPage.setPage(1);
                })
                .catch(function (error) { // 请求失败处理
                    console.log(error);
                });
        }
    }
})


/**
 * 修改卡片的逻辑
 */
var updateCard = new Vue({
    el:'#vue-el-updateCard',
    data:{
        cardId:null,
        updateCardId: null,
        cardAddress:null,
        cardName:null,
        cardChancef:null,
        cardChancel:null,
        cardChance:null,
        cardLevel:null,
        gameName:null,
        message:null,
    },
    methods:{
        deleteParams: function(){
            params.delete("cardId");
            params.delete("cardChancef");
            params.delete("cardChancel");
            params.delete("cardChance");
            params.delete("cardLevel");
            params.delete("gameName");
            params.delete("cardAddress");
            params.delete("cardName");
            params.delete("updateCardId");
        },

        setParams: function(){
            params.append("cardName",this.cardName);
            params.append("cardAddress",this.cardAddress);
            if(this.cardId != null)
                params.append("cardId",this.cardId);
            if(this.cardChancef != null)
                params.append("cardChancef",this.cardChancef);
            if(this.cardChancel != null)
                params.append("cardChancel",this.cardChancel);
            if(this.updateCardId != null)
            params.append("updateCardId",this.updateCardId);
            params.append("cardChance",this.cardChance);
            params.append("cardLevel",this.cardLevel);
            params.append("gameName",this.gameName);
        },

        updateCardinfo: function(){
            this.deleteParams();
            this.setParams();
            //向后台传递数据, 修改数据
            this.getCardPaging(params);
        },
        getCardPaging: function (params) {
            axios
                .post('/ajax/card/update',params)
                .then(function (response){
                    info.cardPaging.data.message = response.data.message;
                    updateCard.message = response.data.message;
                    //刷新当前页面
                    updateCard.deleteParams();
                    setPage.setPage(data.page);
                })
                .catch(function (error) { // 请求失败处理
                    console.log(error);
                });
        }
    }
})


/**
 * 删除卡片逻辑
 */
var deleteCard = new Vue({
    el:'#vue-el-deleteCard',
    data:{
        cardId:null,
        message:null,
    },
    methods:{
        deleteParams: function(){
            params.delete("cardId");
        },

        setParams: function(){
            if(this.cardId != null)
                params.append("cardId",this.cardId);
        },

        deleteCardCardinfo: function(){
            this.deleteParams();
            this.setParams();
            //向后台传递数据, 修改数据
            this.getCardPaging(params);
        },
        getCardPaging: function (params) {
            axios
                .post('/ajax/card/delete',params)
                .then(function (response){
                    info.cardPaging.data.message = response.data.message;
                    deleteCard.message = response.data.message;
                    //刷新当前页面
                    deleteCard.deleteParams();
                    setPage.setPage(data.page);
                })
                .catch(function (error) { // 请求失败处理
                    console.log(error);
                });
        }
    }
})

var addCardmessageNull = new Vue({
    el:'#addData',
    data:{

    },
    methods:{
        setMessageNull:function () {
            addCard.message = null;
        }
    }
})